﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using CPN.PJN.Silverlight.Common.Helpers;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CPN.PJN.Common.Helpers.UnitTest0s
{
    [TestClass]
    public class HelpersUnitTests : SilverlightTest
    {
        public enum EnumTest
        {
            Test1,
            Test2,
            Test3
        }

        [TestMethod]
        public void UnitTest01_GetValues()
        {
            var enumValues = typeof(EnumTest).GetValues();
            Assert.AreEqual(3, enumValues.Count());
            Assert.AreEqual("Test1", enumValues[0]);
            Assert.AreEqual("Test2", enumValues[1]);
            Assert.AreEqual("Test3", enumValues[2]);
        }

        [TestMethod]
        public void UnitTest02_ToObservableCollection()
        {
            var list = new List<string>
            {
                "Test1",
                "Test2",
                "Test3"
            };

            var listObservable = list.ToObservableCollection();

            Assert.AreEqual(list.Count, listObservable.Count);
        }

        [TestMethod]
        public void UnitTest03_AddRange()
        {
            var collection = new ObservableCollection<string>
            {
                "Test1",
                "Test2",
                "Test3"
            };

            var list = new List<string>
            {
                "Test4",
                "Test5"
            };

            collection.AddRange(list);

            Assert.AreEqual(5, collection.Count);
        }

        [TestMethod]
        public void UnitTest04_FindTransform()
        {
            Ellipse ellipse = XamlReader.Load(@"
                <Ellipse xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml\'>
                    <Ellipse.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX='1.5' ScaleY='0.5' />
                        </TransformGroup>
                    </Ellipse.RenderTransform>
                </Ellipse>") as Ellipse;

            // Teste si on retrouve bien le Transform
            var scaleTransform = ellipse.FindTransform<ScaleTransform>();
            Assert.IsTrue(scaleTransform != null && Math.Round(scaleTransform.ScaleX, 1) == 1.5 && Math.Round(scaleTransform.ScaleY, 1) == 0.5);

            // Teste si on créé bien le Transform
            var rotateTransform = ellipse.FindTransform<RotateTransform>();
            Assert.IsTrue(rotateTransform != null);

            Ellipse ellipseWithNoTransform = XamlReader.Load(@"<Ellipse xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'></Ellipse>") as Ellipse;

            // Teste si on créé bien le TransformGroup contenant un TranslateTransform
            var translateTransform = ellipseWithNoTransform.FindTransform<TranslateTransform>();
            Assert.IsTrue(translateTransform != null);
            Assert.IsTrue(((TransformGroup)ellipseWithNoTransform.RenderTransform).Children[0] is TranslateTransform);
        }

        [TestMethod]
        public void UnitTest05_Visibility()
        {
            Ellipse ellipse = XamlReader.Load(@"<Ellipse xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'></Ellipse>") as Ellipse;

            var isVisibile = ellipse.IsVisible();

            Assert.IsTrue(isVisibile);

            ellipse.SetVisibility(!isVisibile);
            isVisibile = ellipse.IsVisible();

            Assert.IsFalse(isVisibile);
        }

        [TestMethod]
        public void UnitTest06_FindResource()
        {
            Grid grid = XamlReader.Load(@"
                <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                    <Grid.Resources>
                        <Style TargetType='Ellipse' x:Key='TestEllipseStyle'/>
                    </Grid.Resources>
                    <ContentControl>
                        <Ellipse x:Name='TestEllipse' />
                    </ContentControl>
                </Grid>") as Grid;

            var ellipse = grid.FindName("TestEllipse") as Ellipse;

            var ellipseStyle = ellipse.FindResource("TestEllipseStyle");

            Assert.IsTrue(ellipseStyle != null);
        }

        [TestMethod]
        public void UnitTest07_IsLoaded()
        {
            Grid grid = XamlReader.Load(@"
                <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                    <ContentControl>
                        <Ellipse x:Name='TestEllipse' />
                    </ContentControl>
                </Grid>") as Grid;

            var ellipse = grid.FindName("TestEllipse") as Ellipse;
            var isLoaded = ellipse.IsLoaded();

            Assert.IsTrue(isLoaded);
        }

        [TestMethod]
        public void UnitTest08_FindParent()
        {
            Grid grid = XamlReader.Load(@"
                <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                    <ContentControl>
                        <ContentControl x:Name='FirstContentControl'>
                            <Grid>
                                <Ellipse x:Name='TestEllipse' />
                            </Grid>
                        </ContentControl>
                    </ContentControl>
                </Grid>") as Grid;

            var ellipse = grid.FindName("TestEllipse") as Ellipse;
            var firstContentControl = ellipse.FindParent<ContentControl>();

            Assert.IsTrue(firstContentControl.Name == "FirstContentControl");
        }

        [TestMethod]
        [Asynchronous]
        public void UnitTest09_FindChild()
        {
            Grid grid = XamlReader.Load(@"
                <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                    <ContentControl>
                        <ContentControl x:Name='FirstContentControl'>
                            <Grid>
                                <Ellipse x:Name='TestEllipse' />
                            </Grid>
                        </ContentControl>
                    </ContentControl>
                </Grid>") as Grid;

            TestPanel.Children.Add(grid);
            
            Ellipse ellipse = null;
            Deployment.Current.Dispatcher.BeginInvoke(() => ellipse = grid.FindChild<Ellipse>());

            EnqueueConditional(() =>
            {
                return ellipse != null && ellipse.IsLoaded();
            });

            EnqueueCallback(() =>
            {
                Assert.IsTrue(ellipse != null);
                Assert.IsTrue(ellipse.Name == "TestEllipse");
            });

            EnqueueTestComplete();
        }

        [TestMethod]
        public void UnitTest10_RemoveFromParentPanel()
        {
            Grid grid = XamlReader.Load(@"
                <Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
                    <ContentControl>
                        <ContentControl x:Name='FirstContentControl'>
                            <Grid>
                                <Ellipse x:Name='TestEllipse' />
                            </Grid>
                        </ContentControl>
                    </ContentControl>
                </Grid>") as Grid;

            TestPanel.Children.Add(grid);

            var ellipse = grid.FindName("TestEllipse") as Ellipse;

            Assert.IsTrue(VisualTreeHelper.GetParent(ellipse) != null);

            ellipse.RemoveFromParentPanel();

            Assert.IsTrue(VisualTreeHelper.GetParent(ellipse) == null);
        }
    }
}